//
//  GFGeneralManager.m
//  GLPFinance
//
//  Created by ITBE on 16/10/28.
//  Copyright © 2016年 intube. All rights reserved.
//

#import "ITGeneralManager.h"
#import "ITNetWorkRequestManager.h"
#import "AppDelegate.h"
#import "ITAppStartManager.h"
#import "ITNetworkMonitorManager.h"

#define InstallationDateKey @"InstallationDateKey"

static ITGeneralManager *generalManager;
@interface ITGeneralManager(){
    
    BOOL _configRequesting;
    
    DisQueueTask configRequestTask;
    
}

IT_PROPERTY_STRONG NSTimer *monitorDataTimer;

@end

@implementation ITGeneralManager
+ (id)defaultManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (generalManager == nil) {
            generalManager = [[ITGeneralManager alloc] init];
            [generalManager initSetting];
        }
    });
    
    return generalManager;
}

- (void)initSetting{
    //NSString *tmp = [ITDeviceInfoManager.sharedManager getIDFA];
    [self installationDay];
    //self.unitType = [[NSUserDefaults getValueForKey:UnitTypeKey] intValue];
}

- (void)networkChanged:(NSNotification *)obj{
    AFNetworkReachabilityStatus status = [obj.object intValue];
    if (status > 0 ) {
        if (configInfo== nil) {
            [self getCommonConfigInfo:nil];
        }
//        if (!TTADManager.shareManager.isLoadedAd) {
//            [TTADManager.shareManager syncConfigV2];
//        }
    }
}

- (NSString *)configJson{
    return configInfo.allValues.modelToJSONString;
}

-(BOOL)hasConfig{
    return configInfo != nil;
}

- (void)refreshConfig{
    configInfo = nil;
    [self getCommonConfigInfo:nil];
}

- (id)getCommonConfigInfo:(void(^)(id config))callback{
    if (configInfo.count>0) {
        if (callback) callback(configInfo);
        return configInfo;
    }
    if (_configRequesting) {
        if (callback) callback(configInfo);
        return nil;
    }
    _configRequesting = YES;
    @weakify(self);
    
    [[ITNetWorkRequestManager shareManager] get:@"/common/config" parameters:nil callback:^(ITNetWorkModel *ret) {
        @strongify(self);
        NSDictionary *data = ret.data;
        if (data) {
            NSMutableDictionary *info = [[NSMutableDictionary alloc] initWithCapacity:data.count];
            for (NSDictionary *tmp in data) {
                ITCommonConfig *config = [ITCommonConfig modelWithJSON:tmp];
                info[config.code] = config;
            }
            
            self->configInfo = info;
        
            MainDelay(0, ^{
                [NSNotificationCenter.defaultCenter postNotificationName:NotificationDidUpdateConfig object:nil];
            });
            
            [NSNotificationCenter removeObserver:self name:NotificationNetworkChanged object:nil];
            if (callback) callback(info);
            [AppUtils trackMTAEventNo:@"vq_00019" parameters:@{@"duration": @(ret.coast)}];
        }else{
            if (callback) callback(nil);
            [NSNotificationCenter addObserver:self selector:@selector(networkChanged:) name:NotificationNetworkChanged];
        }
        self->_configRequesting = NO;
    } isNotify:NO];
    return nil;
}



- (ITCommonConfig *)configDicWithKey:(NSString *)key{
    if (configInfo == nil) {
        if (!_configRequesting) {
            [self getCommonConfigInfo:nil];
        }
        return nil;
    }
    ITCommonConfig *result = configInfo[key];
    return result;
}

/**意见反馈*/
- (void)postFeedbackWithParams:(NSDictionary *)params callback:(void (^) (BOOL result))callback{
    [[ITNetWorkRequestManager shareManager] post:@"/feedback" parameters:params callback:^(ITNetWorkModel *ret) {
        if (ret.code == 200) {
            if (callback) callback(YES);
        }else{
            if (callback) callback(NO);
        }
    } isNotify:NO];
}

#pragma mark - 安装日期天数

- (long)installationDay{
    NSNumber *num = [NSUserDefaults getValueForKey:InstallationDateKey];
    if (num == nil) {
        long dayCount = NSDate.dayCount;
        [NSUserDefaults saveValue:@(dayCount) forKey:InstallationDateKey];
        return dayCount;
    }else {
        return num.longValue;
    }
}


@end
